package com.cuit.riskassessment.action.isms;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.util.JSONUtils;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.cuit.riskassessment.service.AreaService;
import com.cuit.riskassessment.service.FocManageService;
import com.cuit.riskassessment.util.JsonUtil;
import com.cuit.riskassessment.util.Result;
import com.cuit.riskassessment.util.Tree;
import com.cuit.riskassessment.vo.Area;
import com.cuit.riskassessment.vo.FocManage;

/**
 * Area 操作的相关action
 * 
 * @author Yang_Zhiqin-pc
 * 
 */
@Controller
@RequestMapping(value = "file")
public class FocManageAction {
	final static private String SUCCESS = "success";
	final static private String MESSAGE = "message";
	@Autowired
	@Resource(name = "docService")
	private FocManageService docService;

	/**
	 * TODO 下载文件
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("download")
	public Object download(@RequestParam("name") String fileName,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> res = new HashMap<String, Object>();
		res.clear();
		res.put("path", "操作成功！");
		res.put(SUCCESS, true);
		return res;
	}

	@RequestMapping(value = "/addFile", method = RequestMethod.POST)
	public @ResponseBody
	void addFile(HttpServletResponse response, HttpServletRequest request,
			@RequestParam("title") String name, // 设置请求参数的名称和类型
			@RequestParam("keyword") String keyword, // 设置请求参数的名称和类型
			@RequestParam("describe") String describe, // 设置请求参数的名称和类型
			@RequestParam("file") CommonsMultipartFile mFile) { // 请求参数一定要与form中的参数名对应
		FocManage doc = new FocManage();
		doc.setTitle(name);
		doc.setKeyword(keyword);
		doc.setDescribe(describe);
		String path = mFile.getOriginalFilename();
		doc.setPath(path);
		String pa = request.getRealPath("/");
		doc.setPublisher("author");

		response.setHeader("Accept", "text/html");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");

		Map<String, Object> res = new HashMap<String, Object>();
		res.put(MESSAGE, "操作失败！");
		res.put(SUCCESS, false);
		res.put("errors", true);

		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		File filePath = new File(pa + "uploadFiles");
		if (!filePath.exists()) {
			filePath.mkdir();
		}
		try {
			FileCopyUtils.copy(mFile.getBytes(), new File(pa + "uploadFiles\\"
					+ path));
		} catch (IOException e) {
			e.printStackTrace();
			res.put("errors", true);
			res.put(MESSAGE, "文件写入错误");
			writer.print(JsonUtil.map2json(res));
			writer.flush();
			writer.close();
			return;
		}
		if (this.docService.addDoc(doc)) {
			res.put(MESSAGE, "添加成功");
			res.put(SUCCESS, true);
			res.put("errors", false);
		}

		writer.print(JsonUtil.map2json(res));
		writer.flush();
		writer.close();
		return;
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	Object list(HttpServletResponse response,
			@RequestParam(value = "start") Integer start,
			@RequestParam(value = "limit") Integer limit) {
		response.setHeader("Accept", "application/json");
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("message", "数据读取错误，可能没开服务器哦");
		res.put("success", false);
		List<FocManage> list = docService.classify(start, limit);
		if (list != null) {
			res.put("success", true);
			res.put("root", list);
			res.put("totalCount", docService.getTotalCount());
			res.put("message", "读取成功");
		}
		return res;
	}

	@RequestMapping(value = "/delFiles")
	public @ResponseBody
	Object del(@RequestParam(value = "id") int[] ids) {
		docService.delFiles(ids);
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("success", true);
		res.put("message", "删除成功");
		return res;
	}
	@RequestMapping(value = "/editFile")
	public @ResponseBody
	Object del(FocManage file) {
		docService.edit(file);
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("success", true);
		res.put("message", "删除成功");
		return res;
	}
}
